<template>
  <s-layout class="widthdraw-log-wrap" title="藏品订单" :bgStyle="template.style?.background"
            :navbarStyle="{color:'#1e1e1e'}">
    <su-sticky>
      <my-tabs
        :list="tabDatas"
        @change="tabChange"
        :scrollable="false"
        :current="current"
      ></my-tabs>
      <my-tabs
        :list="tabMaps"
        @change="onChange"
        :scrollable="false"
        :current="state.currentTab"
      ></my-tabs>
    </su-sticky>
    <!-- 记录卡片 -->
    <view class="wallet-log-box ss-p-b-30">
      <view class="log-list" v-for="item in state.pagination.data" :key="item">
        <pending v-show="state.currentTab==0"></pending>
        <buy v-show="state.currentTab==1"></buy>
        <sell v-show="state.currentTab==2"></sell>
        <lose v-show="state.currentTab==3"></lose>
      </view>
    </view>
    <s-empty
      v-if="state.pagination.total === 0"
      icon="/static/comment-empty.png"
      text="暂无数据"
    ></s-empty>
    <uni-load-more
      v-if="state.pagination.total > 0"
      :status="state.loadStatus"
      :content-text="{
        contentdown: '上拉加载更多',
      }"
      @tap="loadmore"
    />
  </s-layout>
</template>

<script setup name="collect">
  import { computed, reactive, ref } from 'vue';
  import sheep from '@/sheep';
  import { onLoad, onReachBottom } from '@dcloudio/uni-app';
  import _ from 'lodash';
  import SLayout from '@/sheep/components/s-layout/s-layout.vue';
  import Pending from '@/pages/collect/components/pending.vue';
  import Lose from '@/pages/collect/components/lose.vue';
  import Sell from '@/pages/collect/components/sell.vue';
  import Buy from '@/pages/collect/components/buy.vue';

  const template = computed(() => sheep.$store('app').template?.home);

  // 顶部tab
  const current = ref(0);
  const tabDatas = [
    {
      name: '首发交易',
      value: 'wallet',
    },
    {
      name: '寄售交易',
      value: 'packet',
    },
  ];

  function tabChange(e) {
    state.currentTab = 0;
    current.vlaue = e.index;
  }

  // tab切换
  const state = reactive({
    currentTab: 0,
    pagination: {
      data: [],
      current_page: 1,
      total: 1,
      last_page: 1,
    },
    loadStatus: '',
  });

  const tabMaps = [
    {
      name: '全部',
      value: 'wallet',
    },
    {
      name: '已通过',
      value: 'packet',
    },
    {
      name: '审核中',
      value: 'transfer',
    }, {
      name: '已拒绝',
      value: 'withdraw',
    },
  ];

  function onChange(e) {
    state.pagination.current_page = 1;
    state.currentTab = e.index;
    getLogList();
  }

  // 获取数据
  async function getLogList(page = 1, list_rows = 5) {
    let status = state.currentTab == 0 ? '' : state.currentTab;
    const res = await sheep.$api.trade.orderLog({ type: 'recharge', status, list_rows, page });
    if (res.code === 1) {
      let logList = _.concat(state.pagination.data, res.data.data);
      state.pagination = {
        ...res.data,
        data: logList,
      };
      if (state.pagination.current_page < state.pagination.last_page) {
        state.loadStatus = 'more';
      } else {
        state.loadStatus = 'noMore';
      }
    }
  }

  //获取系列
  const series = ref([]);
  const getAllSeries = async () => {
    const { code, data } = await sheep.$api.app.brand.allSeries();
    if (code === 1) {
      series.value = data;
    }
  };

  // 加载更多
  function loadmore() {
    if (state.loadStatus !== 'noMore') {
      getLogList(state.pagination.current_page + 1);
    }
  }

  onLoad(() => {
    getLogList();
    getAllSeries();
  });
  onReachBottom(() => {
    loadmore();
  });
</script>

<style lang="scss" scoped>
  // 记录卡片

  .primary-color {
    color: #00A1FF;
  }

  .warning-color {
    color: #F17126;
  }

  .danger-color {
    color: #FF0000;
  }

  .success-color {
    color: #13C093;
  }
</style>
